<template>
	<div class="system-menu-container layout-pd">
		<el-card shadow="hover">
			<div class="system-menu-search mb15">
				<el-button size="default" type="primary" plain class="ml10" @click="onOpenAddMenu">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增菜单
				</el-button>
			</div>
			<el-table
				:data="state.tableData.data"
				v-loading="state.tableData.loading"
                :header-cell-style="{backgroundColor: '#f8f8f8', color: '#010101'}"
                border
                :default-expand-all="true"
                :row-key="(row) => row.code"
                size="small"
				style="width: 100%"
				row-key="code"
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
			>

                <el-table-column type="index" label="序号" width="80" />
				<el-table-column label="菜单名称" prop="name" show-overflow-tooltip>
				</el-table-column>
				<el-table-column prop="code" label="权限代码" show-overflow-tooltip></el-table-column>
				<el-table-column prop="sort" label="排序" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="140">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="onOpenAddMenu('add', scope.row)">新增</el-button>
						<el-button size="small" text type="primary" @click="onOpenEditMenu('edit', scope.row)">修改</el-button>
						<el-button size="small" text type="primary" @click="onTabelRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<MenuDialog ref="menuDialogRef" @refresh="getTableData()" :menuData="state.tableData.data" />
	</div>
</template>

<script setup name="systemMenu">
import { defineAsyncComponent, ref, onMounted, reactive } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useMenuApi } from "/src/api/permission/menu";
// import { setBackEndControlRefreshRoutes } from "/@/router/backEnd";

// 引入组件
const MenuDialog = defineAsyncComponent(() => import('/@/views/permission/menu/dialog.vue'));

const menuApi = useMenuApi();
// 定义变量内容
const menuDialogRef = ref();
const state = reactive({
	tableData: {
		data: [],
		loading: true,
	},
});

// 获取路由数据，真实请从接口获取
const getTableData = () => {
	state.tableData.loading = true;
    menuApi.getMenu().then(res => {
        state.tableData.data = res.data || [];
    }).finally(() => {
        state.tableData.loading = false;
    });
};
// 打开新增菜单弹窗
const onOpenAddMenu = (type, row) => {
	menuDialogRef.value.openDialog('add', row);
};
// 打开编辑菜单弹窗
const onOpenEditMenu = (type, row) => {
	menuDialogRef.value.openDialog(type, row);
};
// 删除当前行
const onTabelRowDel = (row) => {
	ElMessageBox.confirm(`此操作将永久删除路由：${row.name}(${row.code}), 是否继续?`, '提示', {
		confirmButtonText: '删除',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
            menuApi.deleteMenu({id: row.id}).then(() => {
                ElMessage.success('删除成功');
                getTableData();
                //await setBackEndControlRefreshRoutes() // 刷新菜单，未进行后端接口测试
            })
		})
		.catch(() => {});
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>
